using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace GameEditor.Common.Controls
{

    public interface IGroupElement
    {
        bool Checked { get;set;}
    }
    public class GroupControlChangeEventArgs : CancelEventArgs
    {
        private IGroupElement newControl;

        public IGroupElement NewControl
        {
            get { return newControl; }
        }
        public GroupControlChangeEventArgs(IGroupElement element)
        {
            newControl = element;
        }
    }
    public class ControlGroup
    {
        public event CancelEventHandler BeforeToggling = delegate { };
        public event EventHandler AfterToggled = delegate { };
        private IGroupElement activeElement;

        public IGroupElement ActiveElement
        {
            get { return activeElement; }
            set
            {
                IGroupElement old = activeElement;
                if (value != null)
                {
                    GroupControlChangeEventArgs e = new GroupControlChangeEventArgs(value);
                    BeforeToggling(this, e);
                    if (e.Cancel)
                        return;

                    activeElement = value;
                    activeElement.Checked = true;

                    AfterToggled(this, EventArgs.Empty);
                }
                else
                    activeElement = null;

                if (old != null)
                    old.Checked = false;

            }
        }

    }
}
